import { Component, OnInit, Output, EventEmitter, Input } from '@angular/core';
import { appSynonymUrl } from '../../synonym.url';
import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { Service } from '../../../server/server';
import { synonym } from '../../synonym.data';
import { HintsService } from '../../../public/serve/hints/hints.service';
import { InitService } from '../../../public/serve/init/init.service';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
import { LocalDataSource } from 'ng2-smart-table';
import { SmartTableService } from '../../smart-table.service';
declare var $:any;
@Component({
  selector: 'app-synonym-management-details',
  templateUrl: './synonym-management-details.component.html',
  styleUrls: ['./synonym-management-details.component.css']
})
export class SynonymManagementDetailsComponent implements OnInit {

  @Output() onSubmit = new EventEmitter<void>();

  total      : number = 0;
  naturalNum : number = 0;

  tabDetailVal : Array<object>;
  details: object = {};
  @Input() synonymName :any = '';

  isSelectAll : boolean = false;
  isChose : boolean = false;

  constructor(
    private server: Service,
    public request: CudTableService,
    private tableListService: CudTableService,
    private hintsService: HintsService,
    private removeTableService: RemoveTableService,
    public initService: InitService,
    private service11: SmartTableService
  ) { 
    
  }

  ngOnInit() {
  }

  /**
   * 详情
   */
  _details(info: object){
    this.details = info;
    this._tableDetailsList();
  }

  /**
   * 判断是否为空数组
   */ 
  _isNameEmpty() {
    if (this.synonymName === '') {
      return true;
    }
    return false;
  }

  /**
   * 同义词明细列表
   */
  _tableDetailsList(){
    let data = this._tableDetailData();
    
    this.request.postRequest(this.server.baseUrl +appSynonymUrl.getSynonymDetailList,data).then((res:any) => {
      if (res.result_code === '0000') {
        this.tabDetailVal = res.result;
        this.total = res.length;
        this.naturalNum = synonym.pagination.naturalNum;
      }else{
        this.hintsService.messages(res.message,res.result_code);
      }

      return;
    })
    .catch((err: any)=>{
      this.hintsService.messages("error","error");
      return;
    });
  }

  /**
   * 搜索
   * @param val
   */
  _retrieval(val:string){
    synonym.retrieval.queryCriteria = val.trim();
    this._tableDetailsList();
  }

  /**
   * 新增提交
   */
  createConfirm(){

    let info = 
    {
      token         : synonym.retrieval.token,
      createBy      : this.server.getuserName(),
      synonymId     : this.details['synonymId'],
      keywords      : this.synonymName
    };

    this.request
    .postRequest(this.server.baseUrl +appSynonymUrl.addSynonymDetail,info)
    .then(res=>{
      if(res.result_code === '0000'){
        this._tableDetailsList();
        this.onSubmit.emit();
        this.synonymName = '';
      }

      this.hintsService.messages(res.message,res.result_code);
      return;
    })
    .catch((err: any) => {
      this.hintsService.messages('error','error');
      return;
    });
  }

  /**
   * 编辑提交
   * type==1 点击保存，type=0点击编辑
   */
  editConfirm(id,keywords:any){
    
    if(id){
      let info = 
      {
        token         : synonym.retrieval.token,
        createBy      : this.server.getuserName(),
        id            : id,
        keywords      : keywords
      };
      this.request
      .postRequest(this.server.baseUrl +appSynonymUrl.updateDetailSynonym,info)
      .then((res: any) => {
        if(res.result_code == "0000"){
          this._tableDetailsList();
          this.onSubmit.emit();

          this.cancel(id);
        }
  
        this.hintsService.messages(res.message,res.result_code);
      
        return;
      })
      .catch((err: any) => {
        this.hintsService.messages('error','error');
        return;
      });
    }
  }

  /**
   * 点击编辑
   * @param id 
   */
  click_edit(id){
    $("#normal_"+id).attr('class','edit_none');//同义词名称
    $("#edit_"+id).attr('class','normal_block');//文本框
    $("#confirm_normal_"+id).attr('class','edit_none');//编辑和删除按钮
    $("#confirm_edit_"+id).attr('class','normal_block');//保存和取消按钮
  }

  /**
   * 取消
   * @param id 
   */
  cancel(id){
    $("#normal_"+id).attr('class','normal_block');//同义词名称
    $("#edit_"+id).attr('class','edit_none');//文本框
    $("#confirm_normal_"+id).attr('class','normal_block');//编辑和删除按钮
    $("#confirm_edit_"+id).attr('class','edit_none');//保存和取消按钮
  }

  /**
    * 删除
    */
  onDeleteConfirm(id): void {
   
    let info = 
    {
      token      : synonym.retrieval.token,
      ids        : [""+id+""]
    };
    
    this.request
      .postRequest(this.server.baseUrl +appSynonymUrl.deleteSynonymDetails,info)
      .then(res => {
        if(res.result_code == '0000'){
          this._tableDetailsList();
          this.onSubmit.emit();
        }

        this.hintsService.messages(res.message,res.result_code);
        return;
      })
      .catch(err => {
        this.hintsService.messages('error','error');
        return;
      });
  }

  //查询参数
  private _tableDetailData(){
    return {
      token         : synonym.retrieval.token,
      pageNo        : synonym.pagination.pageNumber,
      pageSize      : synonym.pagination.pageSize,
      startTime     : synonym.retrieval.startTime,
      endTime       : synonym.retrieval.endTime,
      queryCriteria : synonym.retrieval.queryCriteria,
      synonymId     : this.details['synonymId'],
    }
  }
}
